package com.jrx.anytxn.transaction.job.dailyAccount.step;

import com.jrx.anytxn.transaction.entity.TlDailyAccountLog;
import com.jrx.anytxn.transaction.entity.TlDisbursementLog;
import com.jrx.anytxn.transaction.job.dailyAccount.bean.DailyAccountBean;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlDailyAccountLogMapper;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlDisbursementLogMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 日终T24核心汇总记账 记账状态更新
 *
 * @Author yhy
 * @Date 2023/4/29 15:23
 */
@Service
public class DailyAccountStatusUpdateWriter implements ItemWriter<DailyAccountBean> {

    @Resource
    private ExtTlDisbursementLogMapper extTlDisbursementLogMapper;

    @Resource
    private ExtTlDailyAccountLogMapper extTlDailyAccountLogMapper;

    @Override
    public void write(List<? extends DailyAccountBean> items) throws Exception {
        if(CollectionUtils.isNotEmpty(items)){
            for (DailyAccountBean dailyAccountBean : items){
                // 批量更新日终T24核心汇总记账明细流水
                List<TlDailyAccountLog> tlDailyAccountLogList = dailyAccountBean.getTlDailyAccountLogList();
                if (CollectionUtils.isNotEmpty(tlDailyAccountLogList)){
                    extTlDailyAccountLogMapper.updateBySeqNo(tlDailyAccountLogList.get(0));
                }

                // 批量更新放款流水表中日终T24核心汇总记账状态
                List<TlDisbursementLog> tlDisbursementLogList = dailyAccountBean.getTlDisbursementLogList();
                if (CollectionUtils.isNotEmpty(tlDisbursementLogList)){
                    for (TlDisbursementLog disbursementLog : tlDisbursementLogList){
                        extTlDisbursementLogMapper.updateByPrimaryKeySelective(disbursementLog);
                    }
                }
            }
        }
    }
}
